package com.wkt.ggkt.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkt.ggkt.model.order.OrderInfo;
import com.wkt.ggkt.order.mapper.OrderDetailMapper;
import com.wkt.ggkt.order.mapper.OrderInfoMapper;
import com.wkt.ggkt.order.service.OrderInfoService;
import com.wkt.ggkt.vo.order.OrderInfoDto;
import com.wkt.ggkt.vo.order.OrderInfoQueryVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 订单表 服务实现类
 * </p>
 *
 * @author wkt
 * @since 2023-12-24
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Override
    public Map<String, Object> findPageOrderInfo(Page<OrderInfo> pageParam, OrderInfoQueryVo orderInfoQueryVo) {
        Map<String, Object> result = new HashMap<>();
        // 1、校验查询参数
        Long userId = orderInfoQueryVo.getUserId();
        String phone = orderInfoQueryVo.getPhone();
        Integer orderStatus = orderInfoQueryVo.getOrderStatus();
        String outTradeNo = orderInfoQueryVo.getOutTradeNo();
        String createTimeBegin = orderInfoQueryVo.getCreateTimeBegin();
        String createTimeEnd = orderInfoQueryVo.getCreateTimeEnd();
        // 2、拼接查询条件
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<>();
        if(orderStatus != null) {
            wrapper.eq("order_status",orderStatus);
        }
        if(userId != null) {
            wrapper.eq("user_id",userId);
        }
        if(!StringUtils.isEmpty(outTradeNo)) {
            wrapper.eq("out_trade_no",outTradeNo);
        }
        if(!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone",phone);
        }
        if(!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time",createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le("create_time",createTimeEnd);
        }

        // 3、分页查询
        Page<OrderInfo> page = baseMapper.selectPage(pageParam, wrapper);
        long totalCount = page.getTotal();
        long pageCount = page.getPages();
        List<OrderInfo> records = page.getRecords();
        // 4、订单里面包含详情内容，封装详情数据，根据订单id查询详情
        List<OrderInfoDto> collect = records.stream().map(data -> {
            OrderInfoDto dto = new OrderInfoDto();
            BeanUtils.copyProperties(data, dto);
            return this.getOrderDetail(dto);
        }).collect(Collectors.toList());
        // 5、所有需要数据封装map集合，最终返回
        result.put("total",totalCount);
        result.put("pageCount",pageCount);
        result.put("records",collect);

        return result;
    }

    private OrderInfoDto getOrderDetail(OrderInfoDto data) {
        Long orderId = data.getId();
        String courseName = orderDetailMapper.getCourseNameByOrderId(orderId);
        data.setCourseName(courseName);
        return data;
    }
}
